package com.caidan.security.web;


import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

@Component
public class JwtUtils {

    private final String secret = "jinbodalao";

    public String createToken(Map<String,Object> map){
        String compact = Jwts.builder()
                .setClaims(map)
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis() + 30 * 60 * 1000))
                .signWith(SignatureAlgorithm.HS256, secret)
                .compact();
        return compact;
    }


    public Claims parseToken(String token){
        Claims body = Jwts.parser()
                .setSigningKey(secret).parseClaimsJws(token)
                .getBody();
        return body;
    }
}
